package org.budo.netty.servlet.server.connector.smart;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Enumeration;

import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;

import org.budo.netty.servlet.server.handler.BudoServletHandler;
import org.budo.netty.servlet.server.handler.servlet.AbstractBudoServletRequest;
import org.budo.support.javax.servlet.BudoServletInputStream;
import org.smartboot.http.HttpRequest;

/**
 * @author lmw
 */
public class SmartHttpRequest extends AbstractBudoServletRequest {
    private HttpRequest smartRequest;

    private SmartHttpConnector httpConnector;

    public SmartHttpRequest(HttpRequest smartRequest, BudoServletHandler servletHandler, SmartHttpConnector httpConnector) {
        this.smartRequest = smartRequest;
        this.httpConnector = httpConnector;

        this.setBudoServletHandler(servletHandler);
    }

    @Override
    public Cookie[] getCookies() {
        org.smartboot.http.server.Cookie[] cookies = this.smartRequest.getCookies();
        if (null == cookies) {
            return null;
        }

        Cookie[] cookieArray = new Cookie[cookies.length];
        for (int i = 0; i < cookies.length; i++) {
            cookieArray[i] = new Cookie(cookies[i].getName(), cookies[i].getValue());
        }
        return cookieArray;
    }

    @Override
    public String getHeader(String name) {
        return this.smartRequest.getHeader(name);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        return Collections.enumeration(this.smartRequest.getHeaders(name));
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        return Collections.enumeration(this.smartRequest.getHeaderNames());
    }

    @Override
    public String getMethod() {
        return this.smartRequest.getMethod();
    }

    @Override
    public String getRequestURI() {
        return this.smartRequest.getRequestURI();
    }

    @Override
    public String getProtocol() {
        return this.smartRequest.getProtocol();
    }

    @Override
    public String getScheme() {
        return this.smartRequest.getScheme();
    }

    @Override
    public int getServerPort() {
        return this.httpConnector.getPort();
    }

    @Override
    public String getRemoteAddr() {
        return this.smartRequest.getRemoteAddr();
    }

    @Override
    public String getLocalName() {
        return this.smartRequest.getLocalAddress().getHostName();
    }

    @Override
    public String getQueryString() {
        return this.smartRequest.getQueryString();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        InputStream inputStream = this.smartRequest.getInputStream();
        return new BudoServletInputStream(inputStream);
    }

    @Override
    public int getRemotePort() {
        InetSocketAddress remoteAddress = this.smartRequest.getRemoteAddress();
        return remoteAddress.getPort();
    }
}